https://leetcode-cn.com/problems/split-linked-list-in-parts/

class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* head, int k) {
        vector<ListNode*> result(k,nullptr);
        if(!head)
            return result;
        int count = 0;
        ListNode* cur = head;
        while(cur)
        {
            count++;
            cur = cur->next;
        }
        int len = count / k; //每个链表平均元素个数
        int rem = count % k; //余数
        cur = head;
        ListNode *pre  = nullptr;
        for(int i = 0 ; i < k ;i++)
        {
            result[i] = cur;
            //余数可能大于1 
            int templen = rem > 0 ? (len + 1) : len;
            for(int j  = 0 ; j < templen ; j++)
            {
                pre = cur;
                cur = cur->next;
            }
            if(pre) pre->next = nullptr;
            //余数可能大于1 这两个都是用来截取最前面长度较大的链表
            if(rem) rem--;
        }

        return result;
    }
};

